﻿using System.ComponentModel;
using System.Configuration.Install;
using System.ServiceProcess;

namespace WOLGateway.WindowsService
{
    /// <summary>
    /// Provide the ProjectInstaller class which allows 
    /// the service to be installed by the Installutil.exe tool
    /// </summary>
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private readonly ServiceProcessInstaller process;
        private readonly ServiceInstaller service;

        /// <summary>
        /// Initializes a new instance of the <see cref="ProjectInstaller"/> class.
        /// </summary>
        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "Wake On LAN Gateway";
            service.Description = "Use Wake On LAN 'Magic Packets' to wake up computers inside the local network.";
            service.StartType = ServiceStartMode.Automatic;
            service.Committed += OnServiceInstallerCommitted;
            Installers.Add(process);
            Installers.Add(service);
        }

        /// <summary>
        /// Handles the Committed event of the serviceInstaller control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Configuration.Install.InstallEventArgs"/> instance containing the event data.</param>
        private void OnServiceInstallerCommitted(object sender, InstallEventArgs e)
        {
            var controller = new ServiceController(service.ServiceName);
            controller.Start();
        }
    }
}
